﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Portal.Logic;
using Portal.Interface;
using MyLib;
using Portal.Entity;

namespace WebPortal
{
    public partial class Default : System.Web.UI.Page
    {
        public string Default_LogoutConfirm = Resources.lang.Default_LogoutConfirm;
        public string FULL_NAME = "";
        public string DEFAULT_URL = "";
        public string DEFAULT_HEIGHT = ConfigurationManager.AppSettings["FrameHeight"];
        MenuLogic _menu = new MenuLogic();
        IOrg _org = ServiceContainer.Instance().GetService<IOrg>();
        ResourceLogic _resource = new ResourceLogic();
        protected void Page_Load(object sender, EventArgs e)
        {

            //当前菜单Id
            hidId.Value = Request.QueryString["Id"];
            ResourceEntity currentMenu = new ResourceEntity();
            if (string.IsNullOrEmpty(hidId.Value))
            {
                //获取默认菜单为当前菜单
                currentMenu = _menu.GetDefaultMenu(Page.User.Identity.Name);
                hidId.Value = currentMenu.RESOURCEID.ToString();
            }
            else
            {
                currentMenu = _resource.GetEntity(ConvertUtil.ToInt32(hidId.Value));
            }
            DEFAULT_URL = currentMenu.EXT01;
            if (!string.IsNullOrEmpty(currentMenu.EXT04))
            {
                DEFAULT_HEIGHT = currentMenu.EXT04;
            }

            //绑定导航菜单
            List<ResourceEntity> list = new List<ResourceEntity>();
            list = _menu.GetMenuList(0, Page.User.Identity.Name);
            foreach (ResourceEntity ety in list)
            {
                ety.EXT30 = "";
                if (ety.RESOURCEID == currentMenu.RESOURCEID) //默认页
                {
                    ety.EXT30 = "act";
                }
            }
            rptNav.DataSource = list;
            rptNav.DataBind();

            //绑定子菜单
            list = _menu.GetMenuList(currentMenu.RESOURCEID, Page.User.Identity.Name);
            if (list.Count > 0)
            {
                rptMenu.DataSource = list;
                rptMenu.DataBind();
            }
            else //没有子菜单，不显示子菜单栏
            {
                divMenu.Visible = false;
            }
            //获取用户全名
            FULL_NAME = _org.GetUserEntity(Page.User.Identity.Name).CNName;
            //判断是否为编外人员
            if (Page.User.Identity.Name.IndexOf('Z') > 0)
            {
                string IName = Page.User.Identity.Name;
                IName = IName.Substring(IName.IndexOf('Z'), IName.Length - 8);
                TRXN_Logic Tl = new TRXN_Logic();
                FULL_NAME = Tl.GetLevelUser("USERNAME = '" + IName + "'");
            }
            GetEdoctkn();
        }

        protected void rptMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Repeater rpt = e.Item.FindControl("rptChildMenu") as Repeater;
            ResourceEntity dict = e.Item.DataItem as ResourceEntity;
            List<ResourceEntity> list = _menu.GetMenuList(dict.RESOURCEID, Page.User.Identity.Name);
            //对集合排序
            if (list.Count > 0)
            {
                list.Sort((a, b) => { return a.CNNAME.CompareTo(b.CNNAME); });
            }
            rpt.DataSource = list;
            rpt.DataBind();
        }

        //调用Edoc2 webservice
        private void GetEdoctkn()
        {
            try
            {
                WebReference.Edoc2WebService edoc = new WebReference.Edoc2WebService();
                string useraccount = Page.User.Identity.Name.Split('\\')[1].ToString();
                string tkn = "";
                edoc.Org_UserLoginByUserName(useraccount, out tkn);
                LinkEdoc2.HRef = "http://edoc.goldwind.org/default.aspx?fag=workflow&tkn=" + tkn;
                //  LinkEdoc2.HRef = "http://edoc.goldwind.org/WorkFlow/InboxTaskList.aspx?fag=workflow&tkn=" + tkn;
            }
            catch (Exception)
            {
                throw;
            }

        }
    }
}